package com.app.ui.adapter.base;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.wxj.zx.R;

import butterknife.ButterKnife;

/**
 * 含加载更多 加载更多需要设置（setOpenRefresh，setLoadMore）
 * 只是被BaseRecyclerDataAdapter继承
 * Created by guom on 2016/11/20.
 */

public abstract class BaseRecyclerViewAdapter<VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {
    //------------------------设置RecyclerView------------------------------------
    public void setRecyclerViewType(Context contex, RecyclerView recyclerview, int type) {
        switch (type) {
            case 1:
                //线性布局
                LinearLayoutManager layoutManager = new LinearLayoutManager(contex);
                recyclerview.setLayoutManager(layoutManager);
                recyclerview.setHasFixedSize(true);
                break;
        }

    }

    public void setItemDecoration(RecyclerView recyclerview, int type) {
        switch (type) {
            case 1:
                //添加分割线
                recyclerview.addItemDecoration(new RVSpaceItemDecoration(20));
                break;
        }

    }


    //------------------父类方法（勿动）-------------------
    @Override
    public int getItemCount() {
        int count = getChildCount();
        if (isLoadMore) {
            count += 1;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        int itemViewType;
        //显示加载更多
        if (position == getChildCount()) {
            itemViewType = VIEW_TYPE_TLOAD_MORE;
        } else {
            itemViewType = getChildViewType(position);
        }
        return itemViewType;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        VH vh;
        if (viewType == VIEW_TYPE_TLOAD_MORE) {
            //加载更多ui
            View view = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.footer_loading_small, parent, false);
            vh = (VH) new BaseRecycler(view);
        } else {
            //item
            vh = onCreateHolder(parent, viewType);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        //是加载更多
        if (position == getChildCount()) {
            return;
        }
        onCreateData(holder, position);
        if (!isOnItemClick) {
            return;
        }
        holder.itemView.setOnClickListener(new OnClick(position, -1));
    }

    //---------------------子类方法（需要子类去实现）----------------------------
    //子类必须使用此方法 （list.size()）
    protected int getChildCount() {
        return 0;
    }

    //子类需要设置ui类型的话使用此方法
    protected int getChildViewType(int position) {
        return super.getItemViewType(position);
    }

    //子类必须使用此方法
    protected abstract VH onCreateHolder(ViewGroup parent, int viewType);


    //子类必须使用此方法
    protected abstract void onCreateData(VH holder, int position);


    //---------------------需要子类的Hodler去继承----------------------
    public class BaseRecycler extends RecyclerView.ViewHolder {
        public BaseRecycler(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //---------------------item是否有点击事件---------------
    protected boolean isOnItemClick;

    public void setOnItemClickListener(boolean isOnItemClick) {
        this.isOnItemClick = isOnItemClick;
    }

    public class OnClick implements View.OnClickListener {
        private int arg0, arg1;

        public OnClick(int arg0) {
            this.arg0 = arg0;
        }

        public OnClick(int arg0, int arg1) {
            this.arg0 = arg0;
            this.arg1 = arg1;
        }

        @Override
        public void onClick(View view) {
            onViewClick(view, arg0, arg1);
        }

    }

    public void onViewClick(View view, int arg0, int arg1) {
    }

    //------------------------------设置加载------------------------------
    private SwipeRefreshLayout swipeLayout;

    public void setOpenRefresh(RecyclerView recyclerview) {
        //加载更多
        recyclerview.addOnScrollListener(new ScrollListener());
        ViewParent view = recyclerview.getParent();
        if (view == null) {
            return;
        }
        //下拉刷新
        if (view instanceof SwipeRefreshLayout) {
            swipeLayout = (SwipeRefreshLayout) view;
            swipeLayout.setColorSchemeColors(0xff28B28E);
            swipeLayout.setOnRefreshListener(new OnRefresh());
        }
    }

    //设置下拉刷新
    public void setOpenRefresh(SwipeRefreshLayout swipeLayout) {
        swipeLayout.setColorSchemeColors(0xff28B28E);
        swipeLayout.setOnRefreshListener(new OnRefresh());
    }

    //--------------------------下拉刷新------------------------------
    class OnRefresh implements SwipeRefreshLayout.OnRefreshListener {

        @Override
        public void onRefresh() {
            onLoadingListener.onLoading(true);
        }
    }

    //---------------------是否加载更多---------------------------
    private boolean isLoadMore;
    private boolean isLoadRuning;
    private final int VIEW_TYPE_TLOAD_MORE = 99;

    // 是否要加载更多
    public void setLoadMore(boolean isLoadMore) {
        //加载更多
        this.isLoadMore = isLoadMore;
        notifyDataSetChanged();
    }

    //刷新完成 或加载完成
    public void onRenovationComplete() {
        isLoadRuning = false;
        // 刷新完成
        if (swipeLayout != null && swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
    }

    //下拉监听
    class ScrollListener extends RecyclerView.OnScrollListener {
        private LinearLayoutManager linearLayoutManager;
        private RecyclerView.Adapter adapter;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            //    DLog.e("test", "StateChanged = " + newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (!isLoadMore) {
                return;
            }
            if (isLoadRuning) {
                return;
            }
            RecyclerView.LayoutManager layoutManager = null;
            if (adapter == null) {
                adapter = recyclerView.getAdapter();
            }
            if (linearLayoutManager == null) {
                layoutManager = recyclerView.getLayoutManager();
            }
            if (linearLayoutManager == null && layoutManager == null) {
                return;
            }
            if (layoutManager != null && (layoutManager instanceof LinearLayoutManager)) {
                linearLayoutManager = (LinearLayoutManager) layoutManager;
                return;
            }
            if (linearLayoutManager == null) {
                return;
            }
            int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
            if (lastVisibleItemPosition + 1 == getItemCount() && onLoadingListener != null) {
                //可以加载更多
                isLoadRuning = true;
                onLoadingListener.onLoading(false);
            }
        }
    }

    private OnLoadingListener onLoadingListener;

    public void setOnLoadingListener(OnLoadingListener onLoadingListener) {
        this.onLoadingListener = onLoadingListener;
    }

    /**
     * 刷新回调
     */
    public interface OnLoadingListener {
        /**
         * 暂时只有加载更多
         *
         * @param isRefresh true 刷新 false 加载更多
         */
        void onLoading(boolean isRefresh);

    }
}
